<?php
/**
 * @file indigo/framework/classes/Config.php
 * Contains the definition of the Config class and declares the MissingConfigException that it throws
 * Imports Exception class from the global namespace
 */
 
  namespace indigo\framework;
  use Exception;
  
  /**
   * @class MissingConfigException
   * Thrown when the config file is not found when the Config class tries to load it
   */
  class MissingConfigException extends Exception {}
  
 /**
  * @class Config
  * Loads the config file (config.php) and provides an interface to the configuration options defined in the file
  */
  class Config
  {

   /**
    * The configuration data comprised of all the defined variables in the config.php file
    */
		private $_data = array();    
    
   /**
    * The static singleton instance holder for the Config class
    */
    private static $_instance = null;

   /**
    * Loads config.php and gets puts all defined variables in the file in the private #$_data property
    */
    private function __construct() {
      require_once 'config.php';
      $this->_data = get_defined_vars();
    }
    
   /**
    * Loads the config file
    *
    * This function checks for the existence of config.php and returns the singleton instance of the Config class
    *
    * @returns Returns the singleton instance of the Config class if config.php is found; returns null if otherwise.
    */
    public static function load()
    {
      if (file_exists('config.php')) {
        if(Config::$_instance == null) {
          Config::$_instance = new Config();
        }
        return Config::$_instance;
      } else {
        return null;
      }
    }
    
   /**
    * Retrieves a key from the #$_data property if it exists
    *
    * @param $key the key or variable name of the configuration option you wish to access
    *
    * @returns Returns the value of the configuration option in defined or null if otherwise
    */
		public function get($key)
		{
      if (isset($this->_data[$key])) {
        return $this->_data[$key];
      }
		}
  }